import hashlib
from datetime import datetime

class Block:
    """
        区块结构
            prev_hash: 父区块的哈希值
            transaction: 区块内容  / 交易内容
            timestamp: 区块创建时间
            hash: 区块哈希值
    """
    def __init__(self, transaction, prev_hash):
        self.prev_hash = prev_hash
        self.transaction = transaction
        self.timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        # 计算区块的哈希值
        message = hashlib.sha256()
        message.update(str(self.prev_hash).encode("utf-8"))
        message.update(str(self.transaction).encode("utf-8"))
        message.update(str(self.timestamp).encode("utf-8"))
        self.hash = message.hexdigest()
